package com.liulishuo.magicprogress.demo.widget;

import com.liulishuo.magicprogress.demo.util.Constant;
import com.liulishuo.magicprogresswidget.Imag;
import com.liulishuo.magicprogresswidget.MagValueAnimator;

import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Text;
import ohos.app.Context;

/**
 * 进度条演示页面
 *
 * @since 2012-11-15
 * Created by Jacksgong on 12/11/15.
 */
public class AnimTextView extends Text implements Imag {
    private int progress;
    private int max = Constant.NUM_100;
    private float lastPercent;
    private MagValueAnimator magValueAnimator;
    private long maxTime = Constant.MAX_TIME;

    /**
     * AnimTextView
     *
     * @param context ctx
     */
    public AnimTextView(Context context) {
        super(context);
    }

    /**
     * AnimTextView
     *
     * @param context ctx
     * @param attrSet ast
     */
    public AnimTextView(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    /**
     * AnimTextView
     *
     * @param context ctx
     * @param attrSet ast
     * @param styleName sty
     */
    public AnimTextView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
    }

    /**
     * setProgress
     *
     * @param progress 设置内容
     */
    public void setProgress(final int progress) {
        this.progress = progress;
        setText(String.valueOf(progress));
    }

    public int getProgress() {
        return this.progress;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    @Override
    public float getPercent() {
        return getProgress() / (float) getMax();
    }

    @Override
    public void setPercent(float percent) {
        float pc = percentIn01(percent);
        anim(0, pc, (long) (pc * maxTime));
    }

    @Override
    public void setPercent(float percent, long durationMillis) {
        float pc = percentIn01(percent);
        anim(0, pc, durationMillis);
    }

    @Override
    public void setSmoothPercent(float percent) {
        float pc = percentIn01(percent);
        anim(lastPercent, pc, (long) ((pc - lastPercent) * maxTime));
    }

    @Override
    public void setSmoothPercent(float percent, long durationMillis) {
        float pc = percentIn01(percent);
        anim(lastPercent, pc, durationMillis);
    }

    private void anim(float lastPc, float percent, long duration) {
        if (magValueAnimator != null) {
            if (magValueAnimator.isRunning()) {
                magValueAnimator.end();
            }
            magValueAnimator.release();
        }
        magValueAnimator = MagValueAnimator.ofFloat(lastPc, percent);
        magValueAnimator.setDuration(duration);
        magValueAnimator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                setProgress((int) Math.ceil(value * getMax()));
            }
        });
        magValueAnimator.start();
        this.lastPercent = percent;
    }

    /**
     * percentIn01
     *
     * @param percent 设置值
     * @return pc 返回
     */
    private float percentIn01(float percent) {
        float pc = Math.max(0, percent);
        return pc;
    }
}
